using System;
using System.Configuration;
using System.Diagnostics;
using System.Text;

namespace Needle.Utils {
    /// <summary>
    /// Exception utility
    /// </summary>
    [DebuggerNonUserCode]
    public class Exceptions
    {

        /// <summary>
        /// formats the exception message and the inner exceptions messages.
        /// </summary>
        public static string Format(Exception ex)
        {
            Exception x = ex;
            StringBuilder sb = new StringBuilder();
            while (x != null)
            {
                sb.AppendFormat("{0,-20}: {1}\n", x.GetType().Name.Replace("Exception", ""), x.Message);
                x = x.InnerException;
            }
            return sb.ToString();
        }

        /// <summary>
        /// throws a ConfigurationErrorsException 
        /// </summary>
        public static void ThrowConfigError(string message, ConfigurationElement element)
        {
            throw new ConfigurationErrorsException(message, element.ElementInformation.Source, element.ElementInformation.LineNumber);
        }
        /// <summary>
        /// throws a ConfigurationErrorsException 
        /// </summary>
        public static void ThrowConfigError(string message, Exception inner, ConfigurationElement element)
        {
            throw new ConfigurationErrorsException(message, inner, element.ElementInformation.Source, element.ElementInformation.LineNumber);
        }

    }
}
